package cn.zyn.eduservice.service.impl;

import cn.zyn.eduservice.entity.EduCourse;
import cn.zyn.eduservice.entity.EduCourseDescription;
import cn.zyn.eduservice.entity.vo.CourseInfoVo;
import cn.zyn.eduservice.entity.vo.CoursePublishVo;
import cn.zyn.eduservice.mapper.EduCourseMapper;
import cn.zyn.eduservice.service.EduChapterService;
import cn.zyn.eduservice.service.EduCourseDescriptionService;
import cn.zyn.eduservice.service.EduCourseService;
import cn.zyn.eduservice.service.EduVideoService;
import cn.zyn.servicebase.exceptionhandler.GuliException;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jca.endpoint.GenericMessageEndpointFactory;
import org.springframework.stereotype.Service;

/**
 * <p>
 * 课程 服务实现类
 * </p>
 *
 * @author testjava
 * @since 2022-05-29
 */
@Service
public class EduCourseServiceImpl extends ServiceImpl<EduCourseMapper, EduCourse> implements EduCourseService {

    //注入课程描述
    @Autowired
    private EduCourseDescriptionService courseDescriptionService;
    //注入小节
    @Autowired
    private EduVideoService videoService;
    //注入章节
    @Autowired
    private EduChapterService chapterService;


    @Override
    public String saveCourseInfo(CourseInfoVo courseInfoVo) {
        //1 向课程表添加课程基本信息
        //courseInfoVo转换成eduCourse对象
        EduCourse eduCourse = new EduCourse();
        BeanUtils.copyProperties(courseInfoVo,eduCourse);
        int insert = baseMapper.insert(eduCourse);
        if(insert<=0){
            throw new GuliException(20001,"添加课程信息失败");
        }
        //获取添加之后的课程Id
        String cid = eduCourse.getId();
        //2 向课程简介表添加课程简介
        //edu_course_description
        EduCourseDescription courseDescription = new EduCourseDescription();
        courseDescription.setDescription(courseInfoVo.getDescription());
        courseDescription.setId(cid);
        //BeanUtils.copyProperties(courseInfoVo,courseDescription);
        courseDescriptionService.save(courseDescription);

        return cid;
    }


    //2.根据课程id进行查询课程基本信息
    @Override
    public CourseInfoVo getCourseInfo(String courseId) {
        //2.1 查询课程表
        EduCourse eduCourse = baseMapper.selectById(courseId);
        CourseInfoVo courseInfoVo = new CourseInfoVo();
        BeanUtils.copyProperties(eduCourse,courseInfoVo);
        //2.2查询课程简介表
        EduCourseDescription eduCourseDescription = courseDescriptionService.getById(courseId);
        //courseInfoVo.setDescription(eduCourseDescription.getDescription());
        BeanUtils.copyProperties(eduCourseDescription,courseInfoVo);
        return courseInfoVo;
    }

    
    //3.修改课程信息
    @Override
    public void updateCourseInfo(CourseInfoVo courseInfoVo) {
        //3.1修改课程表
        EduCourse eduCourse =new EduCourse();
        BeanUtils.copyProperties(courseInfoVo,eduCourse);
        int update = baseMapper.updateById(eduCourse);
        if(update == 0){
            throw new GuliException(20001,"修改个人信息失败");
        }
        //3.2修改课程简介表
        EduCourseDescription description = new EduCourseDescription();
        BeanUtils.copyProperties(courseInfoVo,description);
        courseDescriptionService.updateById(description);

    }

    //4.根据课程Id查询课程确认信息
    @Override
    public CoursePublishVo publishCourseInfo(String cid) {
        //调用mapper
        CoursePublishVo publishCourseInfo = baseMapper.getPublishCourseInfo(cid);
        return publishCourseInfo;
    }

    //删除课程
    @Override
    public void removeCourse(String courseId) {
        //根据课程id删除小节
        videoService.removeVideoByCourse(courseId);
        //根据课程id删除章节
        chapterService.removeChapterByCourse(courseId);
        //根据课程id删除简介  简介和课程是一对一的关系
        courseDescriptionService.removeById(courseId);
        //根据课程id删除课程本身
        int result = baseMapper.deleteById(courseId);
        if(result ==0){
            throw new GuliException(20001,"删除失败");
        }
    }

}
